package com.wangxiaotao.Controller;

import com.wangxiaotao.Pojo.*;
import com.wangxiaotao.Service.AdminService;
import com.wangxiaotao.Service.ShopService;
import com.wangxiaotao.WebSocket.WebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class ShopController {
    @Autowired
    ShopService shopService;
    @Autowired
    AdminService adminService;
    @Autowired
    WebSocket webSocket;
    @RequestMapping(value = "shop.do",method = RequestMethod.POST)//加入购物车
    public void shop(int goods_id, HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        String username=user.getUsername();
        Goods goods=shopService.getgoods(goods_id);//获取货物
        List<Shops>shopsList =shopService.getshop(username);//获取订单信息
        Shops shops1=new Shops();
//        System.out.println(shopsList.size());
//        System.out.println(username);
//        System.out.println(goods_id);
        if (shopsList.size()!=0){//用户提交过该商品 商品数量++
            for(int i=0;i<shopsList.size();i++){
                Shops temp_shops=shopsList.get(i);
                if(goods.getGoods_name().equals(temp_shops.getShop_name())) {//同一商品
                    int shop_num=temp_shops.getShop_num();
                    shop_num++;
                    Double shop_price=temp_shops.getShop_price();
                    shops1.setShop_total(shop_num*shop_price);
                    shops1.setUsername(username);
                    shops1.setShop_name(goods.getGoods_name());
                    shops1.setShop_price(goods.getGoods_price());
                    shops1.setShop_num(shop_num);
                    shopService.addshopnum(shops1);//更新商品数量
                    return;//找到商品后 退出循环
                }
            }//不同 商品
            shops1.setUsername(username);
            shops1.setShop_name(goods.getGoods_name());
            shops1.setShop_price(goods.getGoods_price());
            shops1.setShop_total(goods.getGoods_price());
            shopService.addnewshop(shops1);//新加入购物车
        }else{//为空 用户没有提交过该商品
            shops1.setUsername(username);
            shops1.setShop_name(goods.getGoods_name());
            shops1.setShop_price(goods.getGoods_price());
            shops1.setShop_total(goods.getGoods_price());
            shopService.addnewshop(shops1);//新加入购物车
        }
        request.setAttribute("username",username);
    }
    @RequestMapping(value = "setgoods_type.do",method = RequestMethod.POST)
    public String setgoods_type(int goods_type,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        List<Goods>goodsList=shopService.getgoods_type(goods_type);
        request.getSession().setAttribute("goodsList",goodsList);
        request.setAttribute("goodsList",goodsList);
        request.setAttribute("username",user.getUsername());
        request.setAttribute("name",user.getName());
        return "food";
    }
    @RequestMapping(value = "shop_car.do",method = RequestMethod.POST)//遍历购物车
    public String get_all(HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        List<Goods_Shops>goods_shopsList=shopService.setshop_car(user.getUsername());
        request.getSession().setAttribute("goods_shopsList",goods_shopsList);
        request.setAttribute("goods_shopsList",goods_shopsList);
        request.setAttribute("user",user.getName());
        request.setAttribute("username",user.getUsername());
        return "shop_car";
    }
    @RequestMapping(value = "num_change.do",method = RequestMethod.POST)//数量操作
    @ResponseBody
    public void show_num_change(int shop_id, int show_num,Double shop_price,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        Shops shops=new Shops();
        shops.setUsername(user.getUsername());
        shops.setShop_num(show_num);
        shops.setShop_id(shop_id);
        shops.setShop_total(show_num*shop_price);
        shopService.shop_num_change(shops);
    }
    @RequestMapping(value = "delete_shop.do",method = RequestMethod.POST)
    public String delete_shop(int shop_id,HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        Shops shops=new Shops();
        shops.setShop_id(shop_id);
        shops.setUsername(user.getUsername());
        shopService.deleteshop(shops);
        return null;
    }
    @RequestMapping(value = "postshops.do",method = RequestMethod.POST)//提交订单
    public void setfood(HttpServletRequest request){
        User user=(User)request.getSession().getAttribute("user");
        Order order=new Order();
        Order order_num=adminService.getordernum();//获取订单号最大的
        int num;
        if(order_num==null){//还没有订单
            num=0;
        }else{
            num=(Integer.parseInt(order_num.getOrder_num()));
        }
        num++;
        String endnum=String.format("%04d",num);//处理订单号
        order.setOrder_num(endnum);
        order.setName(user.getName());
        order.setAddress(user.getAddress());
        order.setTel(user.getTel());
        int count=shopService.getallorders(user.getUsername());//计算订单总数
        order.setOrder_count(Integer.toString(count));
        webSocket.sendmessage(user.getName());
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        order.setOrder_time(df.format(new Date()).toString());//写入时间
        Double price=shopService.getallprice(user.getUsername());
        order.setOrder_price(Double.toString(price));
        adminService.addoeder(order);
        shopService.postshops(user);//设置购物车标志位
    }
}
